#include "../../include/httpLib.hpp"
const std::string& wwwroot = "./wwwroot/";
extern Log lg;
std::string requestToStr(const httpRequest& req)
{
    std::string str;
    static std::string tail = "\r\n";
    str += req._method + ' ' + req._path + ' ' + req._version + tail;
    for (const auto& [k, v] : req._parms)
        str += k + ": " + v + tail;
    for (const auto& [k, v] : req._headers)
        str += k + ": " + v + tail;
    str += tail;
    str += req._content;
    return str;
}
int main()
{
    lg.startUpLevel(Error);
    httpServer svr(8080, 25, defaultMaxTimeOut, 5);
    svr.setBaseDir(wwwroot);
    svr.Get("/hello", [](const httpRequest& req, httpResponse* rsp) {
        rsp->SetContent(requestToStr(req));
        // sleep(10);
        });
    svr.Post("/login", [](const httpRequest& req, httpResponse* rsp) {
        rsp->SetContent(requestToStr(req));
        });
    svr.Put("/123.txt", [](const httpRequest& req, httpResponse* rsp) {
        std::string path = wwwroot + "123.txt";
        Util::writeFile(path, req._content);
        });
    svr.Delete("/delete", [](const httpRequest& req, httpResponse* rsp) {
        rsp->SetContent(requestToStr(req));
        });
    svr.init();
    svr.start();
    return 0;
}
